//
//  SSNetWorkTool.m
//  Gift
//
//  Created by 刘珊珊 on 15/12/1.
//  Copyright © 2015年 刘珊珊. All rights reserved.
//

#import "SSNetWorkTool.h"

@implementation SSNetWorkTool

+ (void)getWithUrl:(NSString *)url withParameter:(NSDictionary *)parameter withHttpHeader:(NSDictionary *)header withResponseType:(ResponseType)responseType withSuccess:(SuccessBlock)success withFail:(FailBlock)fail{
    // 1.初始化sessionManager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.处理请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // 3.判断返回值数据类型
    switch (responseType) {
        case ResponseTypeDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ResponseTypeJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ResponseTypeXML:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
    }
    // 4.判断返回值所接收的具体类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    // 5.get请求
    [manager GET:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (fail) {
            fail(error);
        }
        
    }];
}

+(void)postWithUrl:(NSString *)url withBody:(id)body withBodyType:(BodyType)bodyType withHttpHeader:(NSDictionary *)header withResponseType:(ResponseType)responseType withSuccess:(SuccessBlock)success withFail:(FailBlock)fail{
    // 1.初始化sessionManager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.处理body
    switch (bodyType) {
        case BodyTypeDictionary:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case BodyTypeString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
                return parameters;
            }];
            break;
    }
    // 3.处理请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // 4.判断返回值数据类型
    switch (responseType) {
        case ResponseTypeDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case ResponseTypeJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ResponseTypeXML:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
    }
    // 5.判断返回值所接收的具体类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    // 6.post请求
    [manager POST:url parameters:body success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (fail) {
            fail(error);
        }
        
    }];
}












@end
